package vip.meeet.jdk.concurent;


import java.util.LinkedList;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;


public class MyTaskHelper {

    private static final BlockingQueue<String> DATA_QUEUE = new LinkedBlockingQueue<>(1000);

    private static volatile boolean stop = false;

    private static Thread thread;

    static {
        start();
    }

    public static void addTask(String task) throws InterruptedException {
        DATA_QUEUE.put(task);
    }

    public static synchronized void start(){
        if(stop){
            if(thread == null || !thread.isAlive()) {
                thread = new Thread(new SendDataTask(), "MyTaskHelper");
                thread.setDaemon(true);
                thread.start();
            }
            stop = false;
        }
    }

    public static void stop(){
        stop = true;
        System.out.println("处理未发送数据逻辑");
    }

    private static class SendDataTask implements Runnable {

        @Override
        public void run() {
            LinkedList<String> datas = new LinkedList<>();
            while (!stop){
                datas.clear();
                DATA_QUEUE.drainTo(datas,100);
                System.out.println("发送日志数据逻辑 datas");
            }
        }
    }

}
